using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Markup;
using Common;
using SExperiment;
using SProject;

namespace MainView.StandardCurve
{
	public partial class SaveStandardCurveWindow : Window, IComponentConnector
	{
		private class StandardDataSource
		{
			public bool IsSelect { get; set; }

			public Project Project { get; set; }

			public string FileName { get; set; }

			public string Version { get; set; }
		}

		private Subset Subset;

		private List<StandardDataSource> list = new List<StandardDataSource>();

		public SaveStandardCurveWindow()
		{
			InitializeComponent();
			textBoxPath.Text = Utility.GetStandardPath();
		}

		public void SetProjects(IList<Project> prjList)
		{
			if (prjList == null)
			{
				return;
			}
			foreach (Project prj in prjList)
			{
				Subset.SubsetParamter subsetParamter = Subset.GetSubsetParamter(prj);
				if (subsetParamter != null && subsetParamter.CurrentProjectStdCurve != null && subsetParamter.CurrentProjectStdCurve.TargetStdCurveList != null && subsetParamter.CurrentProjectStdCurve.TargetStdCurveList.Count > 0)
				{
					list.Add(new StandardDataSource
					{
						IsSelect = true,
						Project = prj,
						FileName = prj.BasicInfo.Name
					});
				}
			}
			dataGrid.ItemsSource = list;
		}

		public void SetSubset(Subset subset)
		{
			Subset = subset;
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			if (!Directory.Exists(textBoxPath.Text))
			{
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_folder_not_exist"));
				textBoxPath.Focus();
				textBoxPath.SelectAll();
			}
			else
			{
				if (Subset == null)
				{
					return;
				}
				List<StandardDataSource> list = this.list.Where((StandardDataSource s) => s.IsSelect).ToList();
				if (list.Count == 0)
				{
					System.Windows.MessageBox.Show(Utility.GetMessage("msg_project_not_select"));
					return;
				}
				if (list.Where((StandardDataSource s) => s.FileName.Equals(string.Empty)).Count() > 0)
				{
					System.Windows.MessageBox.Show(Utility.GetMessage("msg_filename_is_null"));
					return;
				}
				bool flag = false;
				string text = textBoxPath.Text;
				if (text.LastIndexOf("\\") != text.Length - 1)
				{
					text += "\\";
				}
				foreach (StandardDataSource item in list)
				{
					if (!item.IsSelect)
					{
						continue;
					}
					string text2 = text + item.FileName + "." + Experiment.FileStdExt;
					if (File.Exists(text2))
					{
						StringBuilder stringBuilder = new StringBuilder();
						stringBuilder.AppendLine(text2);
						stringBuilder.AppendLine(Utility.GetMessage("msg_standardcurve_file_exist_cover"));
						if (System.Windows.MessageBox.Show(stringBuilder.ToString(), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
						{
							continue;
						}
					}
					Subset.SaveStandardData(item.Project, text2, item.Version);
					flag = true;
				}
				if (flag)
				{
					System.Windows.MessageBox.Show(Utility.GetMessage("msg_standard_save_success"));
				}
			}
		}

		private void btnBrowser_Click(object sender, RoutedEventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			folderBrowserDialog.RootFolder = Environment.SpecialFolder.Personal;
			folderBrowserDialog.SelectedPath = textBoxPath.Text;
			folderBrowserDialog.ShowNewFolderButton = true;
			if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				textBoxPath.Text = folderBrowserDialog.SelectedPath;
			}
		}
	}
}
